home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 1689 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.2 KB

  1. Path: EU.net!sun4nl!ittpub!ittpub!nntp
  2. Newsgroups: comp.lang.c++
  3. Subject: Re: locking
  4. Message-ID: <1996Jan12.104534.1741@ittpub>
  5. From: wil@ittpub.nl (Wil Evers)
  6. Date: 12 Jan 96 10:45:33 WET
  7. References: <4d1kl2$64c@daphne.ecmwf.int>
  8. Distribution: world
  9. Nntp-Posting-Host: lintilla
  10.  
  11. In article <4d1kl2$64c@daphne.ecmwf.int> mab@ecmwf.co.uk (Baudouin Raoult)  
  12. writes:
  13. > [stuff deleted]
  14. > : For the class
  15. > :     struct Foo { void bar(); }
  16. > : a method-invocation like: 
  17. > :     Foo f;
  18. > :     SharedObject<Foo> s(&f);
  19. > :     s->bar(); // (1)
  20. > : results in 
  21. > :         
  22. > :       ((s.operator->()).operator->())->bar(); // = (1)
  23. > : IMHO, the 'LockObject' temporary created in this expression should be
  24. > : destroyed after this line. To force this behavior from an unwilling
  25. > : compiler (;-) one can put (1) in brackets. So the brackets are used to
  26. > : restrict the lifetime of the temporary and not the one of the
  27. > : SharedObject instance.
  28. > :     Enno
  29. > Thanks for showing some nterest to my original posting. I was
  30. > trying to establish a way to wrap/lock any object and them forget about
  31. > it. So the { } are not a satisfactory. It looks like my  compiler
  32. > (cfront 3.??) is not right.
  33. > Baudouin
  34. >
  35.  
  36. Careful here! The question boils down to the lifetime of a temporary, in  
  37. this case the LockObject. When is it destructed? There are differences  
  38. between compilers currently in use: some compilers destruct the temporary  
  39. at the end of the block in which it was created (in which case Enno's  
  40. bracket trick will restrict the lifetime of the lock), but others may  
  41. destruct the temporary right after the call to its operator->(), in which  
  42. case the lock is released *before* the `bar()' member function is invoked.  
  43. I think the draft standard says the temporary will be destructed at the  
  44. `end of the full expression' (that is, an expression that is not part of  
  45. some other expression), so Baudoin Raoult's trick should work, but not all  
  46. compilers are up to date yet.
  47.  
  48. All in all, it is currently impossible to rely on the lifetime of  
  49. temporary if you want to write portable code. I would go for an explictly  
  50. declared instance of the locking object instead - which unfortunately  
  51. requires extra coding and/or interface replication.
  52.  
  53. - Wil
  54.